package com.yaya.common.util.email;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Data
@Component
public class EmailUtil {

    //发件人
    @Value("${spring.mail.username}")
    private String username;

    @Autowired
    private JavaMailSender javaMailSender;

    public void emil(String addressee, String content, String theme) throws MessagingException {

        //创建复杂邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件工具类
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        System.out.println(username+"用户");

        //发件人
        mimeMessageHelper.setFrom(username);

        //主题
        mimeMessageHelper.setSubject(theme);

        //内容
        mimeMessageHelper.setText(content);

        //收件人
        mimeMessageHelper.setTo(addressee);

        //发送
        javaMailSender.send(mimeMessage);


    }

}
